<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5">
{
  style: {
    navigationBarTitleText: '代表详情',
  },
}
</route>
<template>
  <view class="p-8">
    <view class="my-2">
      <view>{{ detailData.name }} {{ detailData.groupName }}</view>
      <view v-if="showTibetan">{{ detailData.nameZw }}</view>
    </view>
    <view class="text-#666 my-2">
      <text class="mr-2">发表时间</text>
      <text class="text-24rpx">{{ detailData.createTime }}</text>
    </view>
    <view>
      <image
        v-if="detailData.avatarUrl"
        :src="detailData.avatarUrl"
        mode="widthFix"
        style="border-radius: 5px"
      ></image>
    </view>
    <p class="my-2">{{ detailData.intro }}</p>
    <p v-if="showTibetan">{{ detailData.introZw }}</p>
    <view class="flex justify-center">
      <view class="content-button" @click="toProtocol">
        <span>我对代表说句话</span>
        <span v-if="showTibetan">བོད་ཀྱི་སྐདབོད་ཀྱི་སྐད</span>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onLoad, onShow, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/store/user'
import { representativeById, repPositionList } from '@/api/representative'

const store = useUserStore()
// 获取环境变量
const showTibetan = import.meta.env.VITE_SHOW_TIBETAN === 'true'
const appTitle = ref(import.meta.env.VITE_APP_TITLE || 'Unibest Login')
const appLogo = ref(import.meta.env.VITE_APP_LOGO || '/static/logo.png')
const appBackground = ref(import.meta.env.VITE_APP_LOGO || '/static/bg.png')
const appBackgroundColor = ref(import.meta.env.VITE_APP_LOGO || '/static/bgcolor.png')

const isLoading = ref(false) // 避免重复请求
const positionList = ref<any[]>([])

const detailId = ref(null)
const detailData = ref<any>({})
const tenantId = ref(null)
onLoad(async (options) => {
  console.log('options', options)
  tenantId.value = options?.tenantId
  detailId.value = options?.id
  console.log(' detailId.value', detailId.value)
  await getPositionList()
  if (detailId.value) getrepresentativeById(detailId.value)
})

const toProtocol = () => {
  uni.navigateTo({ url: `/pages/index/protocol?tenantId=2` })
}
const getPositionList = async () => {
  try {
    const res = await repPositionList()
    if (res.code === 0) {
      positionList.value = res.data
    }
  } catch (error) {}
}
//获取列表数据
const getrepresentativeById = async (representativeId: number) => {
  try {
    const res = await representativeById(representativeId)
    if (res.code === 0) {
      detailData.value = res.data || {}
      if (detailData.value.groupId) {
        detailData.value.groupName = positionList.value.find(
          (groupItem) => groupItem.groupId === detailData.value.groupId,
        ).groupName
      }
    }
  } catch (error) {
    console.error(error)
  } finally {
    isLoading.value = false
  }
}
</script>

<style lang="scss">
.content-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 40rpx;
  font-size: 16px;
  width: 300rpx;
  height: 160rpx;
  text-align: center;
  // padding: 0 5px;
  border: 1px solid #ccc;
  color: #000;
}
</style>
